package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.resutl.RestResponse;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;

import com.xuecheng.content.service.ICourseBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: CourseBaseInfoController
 * @Description:
 * @Author: Huang Yao
 * @CreateTime: 2024-06-22 17:55
 * @Version: 1.0
 **/
@RestController
@Slf4j
@Api(tags = "课程信息编辑接口")
public class CourseBaseInfoController {

    @Autowired
    private ICourseBaseService iCourseBaseService;

    @Autowired
    private ApplicationContext applicationContext;


    /**
     * 课程查询
     * @param pageParams 分页参数
     * @param queryCourseParamsDto 分页条件
     * @return com.xuecheng.base.model.PageResult<com.xuecheng.content.model.po.CourseBase>
     */
    @ApiOperation("课程内容分页")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @RequestMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams,
                                       @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        return iCourseBaseService.queryCourseBaseList(pageParams,queryCourseParamsDto);
    }

    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class}) AddCourseDto addCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = 1232141425L;
        return iCourseBaseService.createCourseBase(companyId,addCourseDto);
    }

    @ApiOperation("根据课程id查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        //取出当前用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        return iCourseBaseService.getCourseBaseInfo(courseId);
    }

    /**
     *
     * @param editCourseDto 修改课程
     * @return
     */
    @ApiOperation("修改课程基础信息")
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        //TODO
        Long companyId = 1232141425L;
        CourseBaseInfoDto courseBaseInfoDto = iCourseBaseService.updateCourseBase(companyId, editCourseDto);
        return courseBaseInfoDto;
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/course/{courseId}")
    public RestResponse deleteCourseBase(@PathVariable Long courseId){
        //TODO 机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        log.info("要删除的课程的id:{}",courseId);
        return iCourseBaseService.deleteCourseBase(courseId,companyId);
    }
}
